<?php
namespace Admin\Model;

use Think\Model;

class TrainingModel extends Model
{
    protected $_validate = array(
        array('Name', 'require', '培训名称必填！'), //默认情况下用正则进行验证
        array('Name','/^[a-z0-9\x{4e00}-\x{9fa5}]{2,50}$/u','培训名称必须50个字母以下',0,'regex',3),
        // 在新增的时候验证字段是否唯一
        array('Name','','培训名称已经存在！',0,'unique',1),
        // 在修改时候验证字段是否唯一,需要配合主键
        array('Name,ID','','培训名称已经存在！',1,'unique',2),

    );
    protected $_auto = array(
        array('IsPublishResult', '0'),  // 新增的时候把IsPublishResult字段设置为0
        array('Status', '1'),  // 新增的时候把 Status 字段设置为1
    );

    /**
     * 列表
     * @return mixed 列表
     */
    public function pageList()
    {
        //创建数据模型
        $model = D('Training');
        //排序
        $order = I('order');
        $order_column = $order[0]['column'];    //排序字段从0开始
        $order_dir = $order[0]['dir'];          //asc,desc 升序和降序

        $orderColumn = '';
        if (isset($order_column)) {
            $i = intval($order_column);
            switch ($i) {
                case 0:
                    $orderColumn = 'Name ' . $order_dir;
                    break;
                case 1:
                    $orderColumn = 'RegistrationStartTime ' . $order_dir;
                    break;
                case 2:
                    $orderColumn = 'StartTime ' . $order_dir;
                    break;
                case 3:
                    $orderColumn = 'IsOpen ' . $order_dir;
                    break;
                case 4:
                    $orderColumn = 'TrainingCost ' . $order_dir;
                    break;
                case 5:
                    $orderColumn = 'AllowNumberOfCourses ' . $order_dir;
                    break;
            }
        }
        //搜索
        //$search = I('search')['value'];
        $column = I('columns');
        //分页
        $start = I('start');    //从多少开始
        $length = I('length');  //数据长度
        //表的总记录数
        $recordsTotal = $model->count();
        //条件筛选后记录数
        $recordsFiltered = $recordsTotal;

        //设定查询条件
        $where = '1=1';
        $hasWhere = false;
        if (strlen($column[1]["search"]["value"]) > 0) {
            $hasWhere = true;
            $where = $where . ' AND Name LIKE \'%' . $column[1]["search"]["value"] . '%\'';
        }
        if (strlen($column[4]["search"]["value"]) > 0) {
            $hasWhere = true;
            $where = $where . ' AND IsOpen = ' . $column[4]["search"]["value"];
        }
        if (strlen($column[2]["search"]["value"]) > 1) {
            $hasWhere = true;
            $begin = explode("|",$column[2]["search"]["value"]);
            if(strlen($begin[0])>0) {
                $where = $where . ' AND RegistrationStartTime >= \'' . $begin[0] . '\'';
            }
            if(strlen($begin[1])>0) {
                $where = $where . ' AND RegistrationEndTime <= \'' . $begin[1]. '\'';
            }
        }
        if (strlen($column[3]["search"]["value"]) > 1) {
            $hasWhere = true;
            $begin = explode("|",$column[3]["search"]["value"]);
            if(strlen($begin[0])>0) {
                $where = $where . ' AND StartTime >=\'' . $begin[0]. '\'';
            }
            if(strlen($begin[1])>0) {
                $where = $where . ' AND EndTime <= \'' . $begin[1]. '\'';
            }
        }

        if ($hasWhere) {
            $recordsFiltered = $model->where($where)->count();
        }

        //获取列表
        $list = $model->where($where)->limit($start, $length)->order($orderColumn)->select();
        if($list == null)
        {
            $list=array();
        }
        //格式化数据
        foreach ($list as $k => $v) {
            $list[$k]['IsOpenDesc'] = C('GC.IsOpenText')[$v['isopen']];
            $list[$k]['RegStartTime'] = datetimeShow($v['registrationstarttime']);
            $list[$k]['RegEndTime'] = datetimeShow($v['registrationendtime']);
            $list[$k]['StartTime'] = datetimeShow($v['starttime']);
            $list[$k]['EndTime'] = datetimeShow($v['endtime']);
        }
        //创建返回模型
        $data['data'] = $list;
        $data['recordsTotal'] = $recordsTotal;
        $data['recordsFiltered'] = $recordsFiltered;
        //返回数据
        return $data;
    }
}